package org.lazy.generate;

import java.sql.ResultSetMetaData;
import java.util.List;

import org.lazy.generate.bean.GeneratorContext;
import org.lazy.generate.bean.GeneratorDao;
import org.lazy.generate.bean.GeneratorDataSource;
import org.lazy.generate.bean.GeneratorEntity;
import org.lazy.generate.bean.GeneratorMapper;
import org.lazy.generate.bean.GeneratorService;
import org.lazy.generate.bean.GeneratorTable;
import org.lazy.generate.factory.DaoFactory;
import org.lazy.generate.factory.EntityConditionsFactory;
import org.lazy.generate.factory.EntityFactory;
import org.lazy.generate.factory.MapperFactory;
import org.lazy.generate.factory.ServiceFactory;
import org.lazy.generate.util.DBManagerUtil;
import org.lazy.util.StringUtil;

public class Generator {
	private Generator() {
	}

	public static void build(String resource) {
		Generator generator = new Generator();
		GeneratorConfigXml gx = GeneratorConfigXml.getInstance(resource);
		List<GeneratorContext> targets = gx.getAllContext();
		for (GeneratorContext context : targets) {
			try {
				generator.buildAll(context);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void buildAll(GeneratorContext context) throws Exception {
		GeneratorDataSource dataSource = context.getDataSource();
		String driverClassName = dataSource.getDriverClassName();
		String url = dataSource.getUrl();
		String username = dataSource.getUsername();
		String password = dataSource.getPassword();

		for (GeneratorTable table : context.getTables()) {
			String tableName = table.getTableName();
			if (StringUtil.isBlank(tableName)) {
				throw new Exception(
						"buildDto failed,Cause by:tableName is null");
			}
			ResultSetMetaData metaData = DBManagerUtil.queryTable(
					driverClassName, url, username, password, tableName);

			GeneratorMapper mapper = context.getMapper();
			GeneratorEntity entity = context.getEntity();
			GeneratorDao dao = context.getDao();
			GeneratorService service = context.getService();
			// 创建mapper
			MapperFactory.buildMapperXml(dataSource,mapper, entity, table, metaData);
			MapperFactory.buildMapperXmlExt(dataSource,mapper, entity, table, metaData);
			// 创建Entity和Condition
			EntityFactory.buildEntity(dataSource,entity, table, metaData);
			EntityConditionsFactory.buildEntityCondition(dataSource,entity, table, metaData);
			// 创建Dao、DaoImpl、DaoExt、DaoExtImpl
			DaoFactory.buildDao(dataSource,entity, dao, table, metaData);
			DaoFactory.buildDaoExt(dataSource,entity, dao, table, metaData);
			DaoFactory.buildDaoImpl(dataSource,mapper, entity, dao, table, metaData);
			DaoFactory.buildDaoImplExt(dataSource,entity, dao, table, metaData);
			// 创建Service、ServiceImpl、ServiceExt、ServiceExtImpl
			ServiceFactory.buildService(dataSource,entity, dao, service, table, metaData);
			ServiceFactory.buildServiceExt(dataSource,service, table, metaData);
			ServiceFactory.buildServiceImpl(dataSource,entity, dao, service, table,
					metaData);
			ServiceFactory.buildServiceImplExt(dataSource,service, table, metaData);
		}

	}

}
